import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.MenuElement;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.JScrollPane;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JToolBar;
import javax.swing.JScrollBar;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;


public class SystemInnerFrame extends JFrame {

	private JPanel contentPane;
	private RemoteShellTool sshTool;
	
	/**
	 * Create the frame.
	 */
	public SystemInnerFrame(RemoteShellTool sshTool) {
		this.sshTool = sshTool;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 697, 699);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu menu = new JMenu("操作");
		menuBar.add(menu);
		
		JMenuItem menuItem = new JMenuItem("登出");
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(sshTool.conn != null){
					sshTool.conn.close();
				}
				//e.getComponent().getParent().getParent().getParent().setVisible(false);
				SystemInnerFrame.this.dispose();
				new MainFrame().setVisible(true);
			}
		});
		menu.add(menuItem);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 52, 645, 276);
		contentPane.add(scrollPane);
		
		JTextArea textArea = new JTextArea();
		textArea.setColumns(100);
		textArea.setLineWrap(true);
		scrollPane.setViewportView(textArea);
		
		JButton button = new JButton("读取");
		button.setBounds(281, 20, 62, 23);
		button.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e){
				String content = sshTool.exec("cat "+Constant.fileFullPathName);
				content = content.trim();
				textArea.setText(content);
			}
		});
		contentPane.add(button);
		
		List<Item> items = this.readFile2Item("文本配置.ini");
		JComboBox fileList = new JComboBox();
		fileList.setBounds(10, 21, 257, 21);
		for(Item item:items){
			fileList.addItem(item);
		}
		fileList.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				 switch (e.getStateChange()){
		             case ItemEvent.SELECTED: 
		                 Item t = (Item) e.getItem();
		                 Constant.fileFullPathName = t.getValue();
		                 //System.out.println("选中"+e.getItem()+"> value:"+t.getValue());
		                 break;
	             }
			}
		});
		contentPane.add(fileList);
		
		List<Item> items0 = this.readFile2Item("执行配置.ini");
		JComboBox commandList = new JComboBox();
		for(Item item:items0){
			commandList.addItem(item);
		}
		commandList.setBounds(10, 348, 257, 21);
		commandList.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				 switch (e.getStateChange()){
		             case ItemEvent.SELECTED: 
		                 Item t = (Item) e.getItem();
		                 Constant.commandStr = t.getValue();
		                 //System.out.println("选中"+e.getItem()+" value:"+t.getValue());
		                 break;
	             }
			}
		});
		contentPane.add(commandList);
		
		//执行下面的文本框
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 379, 645, 261);
		contentPane.add(scrollPane_1);
		
		JTextArea textArea_1 = new JTextArea();
		textArea_1.setColumns(100);
		textArea_1.setEditable(false);
		scrollPane_1.setViewportView(textArea_1);
		
		JButton excuButton = new JButton("执行");
		excuButton.setBounds(281, 347, 62, 23);
		excuButton.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e){
				String result = sshTool.exec(Constant.commandStr);
				textArea_1.setText(result);
			}
		});
		contentPane.add(excuButton);

		JButton changeData = new JButton("更新内容");
		changeData.setBounds(357, 20, 93, 23);
		changeData.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e){
				StringBuffer str = new StringBuffer();
				String txt = textArea.getText();
				String[] txts = txt.split("\n");
				for(String txt0:txts){
					str.append(txt0+"\n");
				}
				sshTool.exec("echo -en '"+str.substring(0, str.length()-1)+"' > "+Constant.fileFullPathName);
				String content = sshTool.exec("cat "+Constant.fileFullPathName);
				content = content.trim();
				textArea.setText(content);
			}
		});
		contentPane.add(changeData);
	}
	
	//从配置文件中读取信息
	public List<Item> readFile2Item(String filePath){
		List<Item> itemList = new ArrayList<Item>();
		Item first = new Item("---请选择---", null);
		itemList.add(first);
		String path = new File("").getAbsolutePath()+"\\"+filePath;
		//InputStream in = SystemInnerFrame.class.getClassLoader().getResourceAsStream(filePath);
		InputStream in0 = null;
		try {
			in0 = new FileInputStream(new File(path));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		InputStreamReader inReader = new InputStreamReader(in0);
		BufferedReader reader = new BufferedReader(inReader);
		try {
			String content = "";
			while((content = reader.readLine()) != null){
				if(content != null){
					String[] datas = content.split(":");
					Item item = new Item(datas[0], datas[1]);
					itemList.add(item);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in0.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return itemList;
	} 
}
